package com.linwen.excise.leetcode;

import com.linwen.dataStructure.tree.BinaryTree;
import com.linwen.dataStructure.tree.TreeNode;


import java.util.*;

/**
 * @author : linwen
 * @date : 5:26 下午 2020/10/31
 */
public class _102二叉树的层序遍历 {
    public static void main(String[] args) {
        LinkedList<Integer> inputList = new LinkedList<Integer>
                (Arrays.asList(new Integer[] {3,9,20,null,null,15,7}));
        new BinaryTree();
        TreeNode root = BinaryTree.creatBinaryTree(inputList);
        System.out.println(levelOrder(root));

    }
    public static List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> result = new ArrayList();
        if(root==null){
            return result;
        }
        Queue<Queue> queue = new LinkedList<>();
        Queue<TreeNode> aq = new LinkedList<>();
        aq.add(root);
        queue.add(aq);
        while (!queue.isEmpty()){
            Queue<TreeNode> poll = queue.poll();
            Queue<TreeNode> item = new LinkedList<>();
            List<Integer> list = new ArrayList<>();
            while (!poll.isEmpty()){
                TreeNode node = poll.poll();
                if(node.leftNode!=null){
                    item.add(node.leftNode);
                }
                if(node.rightNode!=null){
                    item.add(node.rightNode);
                }
                list.add(node.data);
            }
            if(!item.isEmpty()){
                queue.add(item);
            }
            result.add(list);
        }

        return result;

    }

}
